package com.zcy;

import com.caucho.hessian.client.HessianProxyFactory;
import com.zcy.entity.EntityInfo;
import com.zcy.service.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

import javax.persistence.Entity;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by join on 2017/6/21.
 */
public class ClientApp {

    String baseUrl = "http://127.0.0.1:18111";
    //String sysModyMetaJson = "{\"class\":{\"-entity-name\":\"SysModfOne\",\"-table\":\"T_SYS_MODF_ONE\",\"id\":{\"-name\":\"id\",\"-type\":\"long\",\"-column\":\"ID\",\"generator\":{\"-class\":\"com.zcy.util.IdentifierGenerator\"}},\"property\":[{\"-name\":\"name\",\"-column\":\"NAME\",\"-type\":\"string\"},{\"-name\":\"address\",\"-column\":\"ADDRESS\",\"-type\":\"string\"},{\"-name\":\"test\",\"-column\":\"TEST\",\"-type\":\"string\"},{\"-name\":\"ke\",\"-column\":\"KE\",\"-type\":\"string\"},{\"-name\":\"ke1\",\"-column\":\"KE1\",\"-type\":\"string\"},{\"-name\":\"status\",\"-column\":\"status\",\"-type\":\"int\"},{\"-name\":\"createDateTime\",\"-column\":\"CREATETIME\",\"-type\":\"timestamp\"},{\"-name\":\"lastModifyDateTime\",\"-column\":\"LASTMODIFY\",\"-type\":\"timestamp\"}]}}";
    String sysModyMetaJson = "{\"class\":{\"entity-name\":\"SysModfOne\",\"table\":\"T_SYS_MODF_ONE\",\"id\":{\"name\":\"id\",\"type\":\"long\",\"column\":\"ID\",\"generator\":{\"class\":\"com.zcy.util.IdentifierGenerator\"}},\"property\":[{\"name\":\"name\",\"column\":\"NAME\",\"type\":\"string\"},{\"name\":\"address\",\"column\":\"ADDRESS\",\"type\":\"string\"},{\"name\":\"test\",\"column\":\"TEST\",\"type\":\"string\"},{\"name\":\"ke\",\"column\":\"KE\",\"type\":\"string\"},{\"name\":\"ke1\",\"column\":\"KE1\",\"type\":\"string\"},{\"name\":\"status\",\"column\":\"status\",\"type\":\"int\"},{\"name\":\"createDateTime\",\"column\":\"CREATETIME\",\"type\":\"timestamp\"},{\"name\":\"lastModifyDateTime\",\"column\":\"LASTMODIFY\",\"type\":\"timestamp\"}]}}";

    HessianProxyFactory factory = new HessianProxyFactory();

    @Before
    public void init(){
        factory.setOverloadEnabled(Boolean.TRUE);
    }

    @Test
    public void testBuildMetaData() throws MalformedURLException {
        IMetaManageService metaManageService = null;

        metaManageService = (IMetaManageService)factory.create(IMetaManageService.class,baseUrl + "/meta/manageAPI");
        //metaManageService.saveOrUpdate(sysModyMetaJson);//不强制重写service类文件
        metaManageService.saveOrUpdate(sysModyMetaJson,Boolean.TRUE);//强制重写service类文件

    }

    @Test
    public void testDyServiceSave() throws MalformedURLException {
        IDyService sysModyService = null;
        sysModyService = (IDyService)factory.create(IDyService.class,baseUrl + "/dy/sysmodfoneAPI");

        Map<String,Object> SysModfOne = new HashMap<String,Object>();
        SysModfOne.put("name","asdasda");
        SysModfOne.put("address","dasdasd");
        SysModfOne.put("test","dasdasd");
        SysModfOne.put("ke","asdasd");
        SysModfOne.put("ke1","asdsadasd");

        sysModyService.save(SysModfOne);
    }

    @Test
    public void testDyServiceFindByHql() throws MalformedURLException{
        IDyService sysModyService = null;
        sysModyService = (IDyService)factory.create(IDyService.class,baseUrl + "/dy/sysmodfoneAPI");
        List<Map<String,Object>>  rs = sysModyService.findAllByHql("from SysModfOne");
        rs.forEach(entity -> {
            entity.forEach((key,val) ->{
                System.out.println(key + "\t" +val);
            });
            System.out.println("####################################################");
        });
    }

    @Test
    public void testDyServiceFindBySql() throws MalformedURLException{
        IDyService sysModyService = null;

        String sql = "select * from t_sys_modf_one tsmo";
        //sql = "select tsmo.name name1,tsmo.address addr, tsmo.id ID, tsmo.ke from t_sys_modf_one tsmo";

        sysModyService = (IDyService)factory.create(IDyService.class,baseUrl + "/dy/sysmodfoneAPI");
        List<Map<String,Object>>  rs = sysModyService.findAllBySql(sql);
        rs.forEach(entity -> {
            entity.forEach((key,val) ->{
                System.out.println(key + "\t" +val);
            });
            System.out.println("####################################################");
        });
    }

}
